<template>
	<u-navbar title="申请历史" left-icon-color="white" :placeholder="true" title-style="color:white" bgColor="#359FE3"
		:autoBack="false" @leftClick="returnIndex"></u-navbar>

	<view v-if="showLianghua" class="tabbar">
		<button class="tab-btn active">申请内容</button>
		<button class="tab-btn" @click="toLianghua">量化标准</button>
	</view>

	<view class="content">
		<uni-forms ref="formData" validateTrigger="blur" :modelValue="formData" label-position="top" :rules="rules">
			<uni-section class="mb-10" title="学生基本信息" titleFontSize="40rpx" :padding="true" type="line">
				<uni-forms-item required label="学生姓名" name="stuName">
					<uni-easyinput disabled type="text" confirmType="next" v-model="formData.stuName" />
				</uni-forms-item>
				<uni-forms-item required label="身份证号" name="stuIdnumber">
					<uni-easyinput disabled type="idcard" confirmType="next" v-model="formData.stuIdnumber" />
				</uni-forms-item>
				<uni-forms-item required label="民族" name="stuNation">
					<z-picker @selectVal="selectNation" placeholder="请选择民族" :itemList="nationList"
						:defaultValue="formData.stuNation" itemKey="nationName" itemLabel="nationName"></z-picker>
				</uni-forms-item>
				<uni-forms-item label="联系电话" name="stuPhone">
					<uni-easyinput disabled type="number" confirmType="next" v-model="formData.stuPhone" />
				</uni-forms-item>

				<uni-forms-item required label="出生日期" name="stuBirthday">
					<u-datetime-picker disabled :show="showDatePicker" :minDate="946656000000"
						:closeOnClickOverlay="true" v-model="formData.stuBirthday" @close="showDatePicker=false"
						@confirm="confirmBirth" @cancel="showDatePicker=false" mode="date"></u-datetime-picker>
					<u-button class="justify-start" @click="showDatePicker = true">{{formData.stuBirthday}}</u-button>
				</uni-forms-item>
				<uni-forms-item required label="入学年月" name="stuInSchoolDate">
					<u-datetime-picker disabled :show="showInSchoolDate" :minDate="946656000000"
						:closeOnClickOverlay="true" v-model="formData.stuInSchoolDate" @close="showInSchoolDate=false"
						@confirm="confirmInSchool" @cancel="showInSchoolDate=false"
						mode="year-month"></u-datetime-picker>
					<u-button class="justify-start"
						@click="showInSchoolDate = true">{{formData.stuInSchoolDate}}</u-button>
				</uni-forms-item>
				<uni-forms-item required label="归属教育局" :label-width="200" name="stuRegion">
					<z-picker @selectVal="selectRegion" placeholder="请选择教育局" itemLabel="name" itemKey="name"
						:defaultValue="formData.stuRegion" :itemList="regionList"></z-picker>
				</uni-forms-item>
				<uni-forms-item required label="在读学校" :label-width="200" name="stuSchool">
					<z-picker @selectVal="selectSchool" placeholder="请选择学校" itemLabel="name" itemKey="name"
						:defaultValue="formData.stuSchool" :itemList="schoolList"></z-picker>
				</uni-forms-item>
				<uni-forms-item required label="当前年级" :label-width="200" name="stuGrade">
					<z-picker @selectVal="selectGrade" placeholder="请选择年级" itemLabel="name" itemKey="name"
						:defaultValue="formData.stuGrade" :itemList="gradeList"></z-picker>
				</uni-forms-item>
				<uni-forms-item required label="当前班级" :label-width="200" name="stuClass">
					<z-picker @selectVal="selectClass" placeholder="请选择班级" itemLabel="name" itemKey="name"
						:defaultValue="formData.stuClass" :itemList="classList"></z-picker>
				</uni-forms-item>

				<uni-forms-item required label="家庭住址" name="stuHomeAddress">
					<u--textarea disabled v-model="formData.stuHomeAddress" placeholder="请输入家庭住址"></u--textarea>
				</uni-forms-item>
				<!-- <uni-forms-item required label="是否申请教育资助" :label-width="200" name="shenqingFlag">
						<u-switch v-model="formData.shenqingFlag" :disabled="true" inactiveValue="0" activeValue="1"></u-switch>
					</uni-forms-item> -->
			</uni-section>

			<uni-section class="mb-10" titleFontSize="40rpx" :padding="true" title="添加监护人" type="line">
				<uni-section titleFontSize="30rpx" :padding="true" title="监护人一" type="square">
					<uni-forms-item :rules="[{'required': true,errorMessage: '监护人姓名必填'}]" required label="姓名"
						label-width="200" name="eShenqingFamilyInfoDTOList[0].fullname">
						<uni-easyinput disabled type="text" confirmType="next"
							v-model="formData.eShenqingFamilyInfoDTOList[0].fullname" />
					</uni-forms-item>
					<uni-forms-item :rules="[{'required': true,errorMessage: '监护人身份证号必填'},{
									validateFunction: validateIdcard,
									errorMessage: '身份证号格式有误！'
								}]" required label="身份证号" label-width="200" name="eShenqingFamilyInfoDTOList[0].idnumber">
						<uni-easyinput disabled type="text" confirmType="next"
							v-model="formData.eShenqingFamilyInfoDTOList[0].idnumber" />
					</uni-forms-item>
					<uni-forms-item
						:rules="[{'required': true,errorMessage: '监护人手机号必填'},{format:'number',errorMessage: '手机号格式错误'}]"
						required label="手机号" label-width="200" name="eShenqingFamilyInfoDTOList[0].phone">
						<uni-easyinput disabled type="text" confirmType="next"
							v-model="formData.eShenqingFamilyInfoDTOList[0].phone" />
					</uni-forms-item>
					<uni-forms-item :rules="[{'required': true,errorMessage: '监护人工作单位必填'}]" required label="工作单位"
						label-width="200" name="eShenqingFamilyInfoDTOList[0].org">
						<uni-easyinput disabled type="text" confirmType="next"
							v-model="formData.eShenqingFamilyInfoDTOList[0].org" />
					</uni-forms-item>
					<uni-forms-item :rules="[{'required': true,errorMessage: '与学生关系必填'}]" required label="与学生关系"
						label-width="200" name="eShenqingFamilyInfoDTOList[0].relation">
						<uni-data-select disabled v-model="formData.eShenqingFamilyInfoDTOList[0].relation"
							:localdata="relationRange"></uni-data-select>
					</uni-forms-item>
				</uni-section>
				<uni-section titleFontSize="30rpx" :padding="true" title="监护人二" type="square">
					<uni-forms-item :rules="[{'required': true,errorMessage: '监护人姓名必填'}]" required label="姓名"
						label-width="200" name="eShenqingFamilyInfoDTOList[1].fullname">
						<uni-easyinput disabled type="text" confirmType="next"
							v-model="formData.eShenqingFamilyInfoDTOList[1].fullname" />
					</uni-forms-item>
					<uni-forms-item :rules="[{'required': true,errorMessage: '监护人身份证号必填'},{
									validateFunction: validateIdcard,
									errorMessage: '身份证号格式有误！'
								}]" required label="身份证号" label-width="200" name="eShenqingFamilyInfoDTOList[1].idnumber">
						<uni-easyinput disabled type="text" confirmType="next"
							v-model="formData.eShenqingFamilyInfoDTOList[1].idnumber" />
					</uni-forms-item>
					<uni-forms-item
						:rules="[{'required': true,errorMessage: '监护人手机号必填'},{format:'number',errorMessage: '手机号格式错误'}]"
						required label="手机号" label-width="200" name="eShenqingFamilyInfoDTOList[1].phone">
						<uni-easyinput disabled type="text" confirmType="next"
							v-model="formData.eShenqingFamilyInfoDTOList[1].phone" />
					</uni-forms-item>
					<uni-forms-item :rules="[{'required': true,errorMessage: '监护人工作单位必填'}]" required label="工作单位"
						label-width="200" name="eShenqingFamilyInfoDTOList[1].org">
						<uni-easyinput disabled type="text" confirmType="next"
							v-model="formData.eShenqingFamilyInfoDTOList[1].org" />
					</uni-forms-item>
					<uni-forms-item :rules="[{'required': true,errorMessage: '与学生关系必填'}]" required label="与学生关系"
						label-width="200" name="eShenqingFamilyInfoDTOList[1].relation">
						<uni-data-select disabled v-model="formData.eShenqingFamilyInfoDTOList[1].relation"
							:localdata="relationRange"></uni-data-select>
					</uni-forms-item>
				</uni-section>


				<uni-forms-item label="共同生活家庭人口数量" required :label-width="250" name="familyCount">
					<uni-easyinput disabled type="number" confirmType="next" v-model="formData.familyCount" />
				</uni-forms-item>
				<uni-forms-item label="家庭年人均收入(元)" required :label-width="250" name="avgSalary">
					<uni-easyinput disabled type="number" confirmType="next" v-model="formData.avgSalary" />
				</uni-forms-item>
			</uni-section>
			<uni-section class="mb-10" titleFontSize="40rpx" :padding="true" title="申请信息" type="line">
				<uni-forms-item label="申请原因" required name="shenqingReason">
					<u--textarea disabled v-model="formData.shenqingReason"
						:rules="[{'required': true,errorMessage: '监护人工作单位必填'}]"
						placeholder="家庭经济情况简要说明(至少填写50字)"></u--textarea>
				</uni-forms-item>
				<uni-forms-item required name="specialType" :rules="[{'required': true,errorMessage: '申请原因必选'}]">
					<text class="block mb-2" v-slot="label">选择家庭经济困难类型</text>
					<u-checkbox-group disabled v-model="specialTypes" placement="column" @change="checkboxChange">
						<u-checkbox :customStyle="{marginBottom: '8px'}" v-for="(item, index) in checkboxList"
							:key="index" :label="item.label" :name="item.value">
						</u-checkbox>
					</u-checkbox-group>
				</uni-forms-item>
			</uni-section>
			<!-- <uni-section class="mb-10" titleFontSize="40rpx" :padding="true" title="监护人确认" type="line">
					<uni-forms-item required label="是否收到资助政策告知书" :label-width="200" name="zhengceNotice">
						<u-switch v-model="formData.zhengceNotice"  :disabled="true" inactiveValue="0" activeValue="1"></u-switch>
					</uni-forms-item>
					<uni-forms-item required label="是否了解资助政策" :label-width="200" name="knowZhengce">
						<u-switch v-model="formData.knowZhengce"  :disabled="true" inactiveValue="0" activeValue="1"></u-switch>
					</uni-forms-item>
					<uni-forms-item required label="监护人是否需要申请" :label-width="200" name="parentShenqing">
						<u-switch v-model="formData.parentShenqing" inactiveValue="0" :disabled="true" activeValue="1"></u-switch>
					</uni-forms-item> -->

			<!-- <z-sign btnText="监护人签字" @saveImgData="saveParentSign"></z-sign> -->
			<!-- <image v-if="formData.parentSign!=null&&formData.parentSign!=''"
						class="w-4_5 h-36 border-2 border-gray-200 m-1" mode="aspectFit" :src="parentSignUrl"></image> -->
			<!-- </uni-section> -->
			<uni-section class="mb-10" titleFontSize="40rpx" :padding="true" title="个人承诺" type="line">
				<text>&nbsp;&nbsp;&nbsp;&nbsp;本人承诺以上所填写材料真实，如有虚假，愿意承担相应责任。</text>
				<z-sign btnText="监护人或学生签字" @saveImgData="saveStudentSign"></z-sign>
				<image v-if="formData.stuSign!=null&&formData.stuSign!=''"
					class="w-4_5 h-36 border-2 border-gray-200 m-1" mode="aspectFit" :src="stuSignUrl"></image>

			</uni-section>
			<uni-section class="mb-10" titleFontSize="40rpx" :padding="true" title="社会救助家庭经济状况核对授权" type="line">
				<text>&nbsp;&nbsp;&nbsp;&nbsp;授权各级学生资助管理机构可以通过社会救助信息平台对收入、财产、支出等情况进行信息查询核对，包括但不限于入户调查和到公安、人社、住建、交通、市场监管、税务、自然资源、公积金中心、银行、保险、证券等部门、机构进行核查和信息比对，该授权当学年内有效。</text>
				<z-sign btnText="所有监护人签字" @saveImgData="saveAllParentSign"></z-sign>
				<image v-if="formData.allParentSign!=null&&formData.allParentSign!=''"
					class="w-4_5 h-36 border-2 border-gray-200 m-1" mode="aspectFit" :src="allParentSignUrl"></image>

			</uni-section>
		</uni-forms>

	</view>


</template>

<script>
	import {
		saveShenqing,
		queryShenqingById,
		orgList,
		queryNationList,
		queryStudent,
		loadImage,
		dictList,
		getBaseUrl
	} from '@/api/login.js'
	import {
		isIdCardNo
	} from '@/utils/common/validation-methods.js'
	import lodashFull from '@/uni_modules/iRainna-lodash/js_sdk/js/lodash.full.min.js'
	const {
		isNil,
		merge,
		cloneDeep
	} = lodashFull()

	export default {
		data() {
			return {
				showDatePicker: false,
				showInSchoolDate: false,
				showLianghua: false,
				checkboxList: [],
				student: {},
				nationList: [],
				regionList: [],
				schoolList: [],
				gradeList: [],
				showEditFamily: false,
				classList: [],
				specialTypes: [],
				zhengceNotice: [1],
				shenqingFlag: [1],
				knowZhengce: [1],
				parentShenqing: [1],
				relationRange: [],
				family: {
					fullname: '',
					age: '',
					relation: '',
					org: '',
					salary: 0,
					health: ''
				},
				formData: {
					//申请ID
					id: '',
					baseUrl: '',
					//学生信息
					studentId: '',
					stuName: '',
					stuIdnumber: '',
					stuNation: '',
					stuPeriod: '',
					stuPhone: '',
					stuRegion: '',
					stuSchool: '',
					stuGrade: '',
					stuClass: '',
					stuBirthday: '',
					stuSex: '',
					stuHomeAddress: '',
					stuInSchoolDate: '',
					//申请信息
					zhengceNotice: 1,
					knowZhengce: 1,
					parentShenqing: 1,
					shenqingFlag: 1,
					shenqingReason: '',
					specialType: '',
					familyCount: 0,
					avgSalary: 0,
					allParentSign: '',
					parentSign: '',
					stuSign: '',
					eShenqingFamilyInfoDTOList: [{

					}, {

					}]
				},
				rules: {
					fullname: {
						rules: [{
							required: true,
							errorMessage: '请填写姓名！'
						}]
					},

					idnumber: {
						rules: [{
								required: true,
								errorMessage: '请填写身份证号！'
							},
							{
								validateFunction: function(rule, value, data, callback) {
									return isIdCardNo(value)
								},
								errorMessage: '身份证号格式有误！'
							}
						]
					},
					stuRegion: {
						rules: [{
							required: true,
							errorMessage: '请选择教育局！'
						}]
					},
					stuSchool: {
						rules: [{
							required: true,
							errorMessage: '请选择学校！'
						}]
					},
					stuGrade: {
						rules: [{
							required: true,
							errorMessage: '请选择学生年级！'
						}]
					},
					stuClass: {
						rules: [{
							required: true,
							errorMessage: '请选择学生班级！'
						}]
					},
					stuBirthday: {
						rules: [{
							required: true,
							errorMessage: '请选择学生生日！'
						}]
					},
					stuInSchoolDate: {
						rules: [{
							required: true,
							errorMessage: '请选择入学年月！'
						}]
					},
					stuHomeAddress: {
						rules: [{
							required: true,
							errorMessage: '请填写家庭住址！'
						}]
					},
					shenqingFlag: {
						rules: [{
							required: true,
							errorMessage: '此项必须勾选！'
						}]
					},
					stuNation: {
						rules: [{
							required: true,
							errorMessage: '请选择民族！'
						}]
					},
				}
			}
		},
		computed: {
			parentSignUrl: function() {
				if (this.formData.parentSign != undefined && this.formData.parentSign.startsWith(
						'data:image/png;base64')) {
					return this.formData.parentSign
				} else {
					return this.baseUrl + this.formData.parentSign
				}
			},
			allParentSignUrl: function() {
				if (this.formData.allParentSign != undefined && this.formData.allParentSign.startsWith(
						'data:image/png;base64')) {
					return this.formData.allParentSign
				} else {
					return this.baseUrl + this.formData.allParentSign
				}
			},
			stuSignUrl: function() {
				if (this.formData.stuSign != undefined && this.formData.stuSign.startsWith('data:image/png;base64')) {
					return this.formData.stuSign
				} else {
					return this.baseUrl + this.formData.stuSign
				}
			}
		},
		watch: {
			// 每当 question 改变时，这个函数就会执行
			zhengceNotice(newValue, oldValue) {
				this.formData.zhengceNotice = newValue != undefined && newValue.length > 0 ? newValue[0] : 0
			},
			knowZhengce(newValue, oldValue) {
				this.formData.knowZhengce = newValue != undefined && newValue.length > 0 ? newValue[0] : 0
			},
			parentShenqing(newValue, oldValue) {
				this.formData.parentShenqing = newValue != undefined && newValue.length > 0 ? newValue[0] : 0
			},
			shenqingFlag(newValue, oldValue) {
				this.formData.shenqingFlag = newValue != undefined && newValue.length > 0 ? newValue[0] : 0
			},
		},
		onLoad(option) {
			if (!isNil(option.studentId)) {
				this.formData.id = option.shenqingId
				this.formData.studentId = option.studentId
				this.student.id = option.studentId
			}

			this.setDefaultValue()
		},
		methods: {
			returnIndex() {
				uni.redirectTo({
					url: '/pages/index/index'
				})
			},
			back() {
				uni.navigateBack({
					delta: 1
				});
			},

			deleteFamily(index) {
				this.formData.eShenqingFamilyInfoDTOList.splice(index, 1);
			},
			async confirmBirth(e) {
				this.showDatePicker = false;
				const timeFormat = uni.$u.timeFormat;
				let timeValue = await timeFormat(e.value, 'yyyy-mm-dd');
				this.formData.stuBirthday = timeValue;
			},
			addFamily() {
				this.showEditFamily = true
			},
			toLianghua() {
				uni.redirectTo({
					url: '/pages/lianghuaView/lianghuaView?studentId=' + this
						.student
						.id +
						"&shenqingId=" + this.formData.id + '&activityId=' + this.formData
						.activityId
				})
			},
			saveParentSign(data) {
				this.formData.parentSign = data
			},
			saveStudentSign(data) {
				this.formData.stuSign = data
			},
			saveAllParentSign(data) {
				this.formData.allParentSign = data
			},
			async confirmInSchool(e) {
				this.showInSchoolDate = false;
				const timeFormat = uni.$u.timeFormat;
				let timeValue = await timeFormat(e.value, 'yyyy-mm');
				this.formData.stuInSchoolDate = timeValue;
			},
			checkboxChange(valueArray) {
				this.formData.specialType = valueArray.join()
			},
			selectRegion(e) {
				console.log('selectRegion');
				this.formData.stuRegion = e.name
				orgList("4", e.id).then(res => {
					this.schoolList = [res.orgList]
				})
			},
			selectSchool(e) {
				console.log('selectSchool');
				this.formData.stuSchool = e.name
				orgList("5", e.id).then(res => {
					this.gradeList = [res.orgList]
				})
			},
			selectGrade(e) {
				console.log('selectGrade');
				this.formData.stuGrade = e.name
				this.formData.stuPeriod = e.master
				orgList("6", e.id).then(res => {
					this.classList = [res.orgList]
				})
			},
			selectClass(e) {
				console.log('selectClass');
				this.formData.stuClass = e.name
			},
			selectNation(e) {
				this.formData.stuNation = e.nationName
			},

			addParentSign() {
				this.showSign = true
			},
			/**
			 * 设置初值
			 */
			async setDefaultValue() {
				this.baseUrl = getBaseUrl()
				//先查询是否已有申请，如果有申请就展示出来，没有申请则查询展示学生信息
				const shenqingRes = await queryShenqingById(this.formData.id)
				if (shenqingRes.success && shenqingRes.shenqing != undefined) {
					this.formData = shenqingRes.shenqing
					if (shenqingRes.shenqing.specialType != undefined && shenqingRes.shenqing.specialType != '') {
						this.specialTypes = shenqingRes.shenqing.specialType.split(",")
						if (this.specialTypes.includes('11') || this.specialTypes.includes('12')) {
							this.showLianghua = true
						}
					}
					this.zhengceNotice = shenqingRes.shenqing.zhengceNotice == '1' ? [1] : []
					this.parentShenqing = shenqingRes.shenqing.parentShenqing == '1' ? [1] : []
					this.knowZhengce = shenqingRes.shenqing.knowZhengce == '1' ? [1] : []
					this.shenqingFlag = shenqingRes.shenqing.shenqingFlag == '1' ? [1] : []

				}
				if (shenqingRes.success && shenqingRes.familyList != undefined && shenqingRes.familyList.length > 0) {
					this.formData.eShenqingFamilyInfoDTOList = shenqingRes.familyList
				}
				//区域选择
				const region_res = await orgList('3')
				if (region_res.success) {
					this.regionList = [region_res.orgList]
				}
				//选择家庭经济困难类型
				const dict_res = await dictList('shenqing_special_type')
				if (dict_res.success) {
					this.checkboxList = dict_res.dictList
				}
				//民族选择
				const res2 = await queryNationList()
				if (res2.success) {
					this.nationList = [res2.nationList]
				}
				//家庭成员关系
				const dict_res2 = await dictList('family_relation')
				if (dict_res2.success) {
					let relationRange = []
					dict_res2.dictList.forEach(dict => {
						relationRange.push({
							text: dict.label,
							value: dict.label
						})
					})
					this.relationRange = relationRange
				}
			}

		}
	}
</script>

<style>
	.nav-title {
		font-size: 40rpx;
		width: 100%;
		text-align: center;
	}

	.uni-forms-item {
		margin: 60rpx auto;
		font-size: 30rpx;
	}

	.content {
		padding: 40rpx;
	}

	.justify-start {
		justify-content: start !important;
	}

	.tabbar {
		display: flex;
		margin: 30rpx 50rpx;
	}

	.tabbar .tab-btn {
		width: 100%;
		height: 70rpx;
		margin: 0;
		padding: 0;
		border: solid 1rpx #eee;
		border-collapse: collapse;
		line-height: 70rpx;
		border-radius: 0;
		background-color: white;
		color: #359FE3;
	}

	.tabbar .tab-btn.active {
		background-color: #359FE3;
		color: white;
	}
</style>